package com.defence.costomapp.adapter;


import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import android.widget.CompoundButton;
import android.widget.TextView;

import com.defence.costomapp.R;
import com.defence.costomapp.activity.statistics.TkMachinListActivity;
import com.defence.costomapp.app.MyApplication;
import com.defence.costomapp.bean.TuikuanMachineBean;
import com.defence.costomapp.utils.SgqUtils;
import com.defence.costomapp.utils.SpUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by author Sgq
 * on 2018/3/21.
 */

public class TkSelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<Serializable> mCheckhis;
    private final List<Serializable> mCheckshop;
    String string;
    private List<TuikuanMachineBean.ListBean> mList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    //     全选和反选
    private boolean isSelectAll = false;
    private int index = 0;

    public TkSelectAdapter(List<TuikuanMachineBean.ListBean> list, String type) {
        if (list == null) {
            throw new IllegalArgumentException("model Data must not be null");
        }
        mList = list;
        string = type;

        mCheckhis = SpUtil.getList(MyApplication.getApp(), "checkhis");
        mCheckshop = SpUtil.getList(MyApplication.getApp(), "checkshop");
    }

    /**
     * 全选和反选
     */
    public void selectAllMain() {
        if (!isSelectAll) {
            for (int i = 0, j = mList.size(); i < j; i++) {
                setItemChecked(i, true);
            }
            index = mList.size();
            isSelectAll = true;
        } else {
            for (int i = 0, j = mList.size(); i < j; i++) {
                setItemChecked(i, false);
            }
            index = 0;
            isSelectAll = false;
        }
        notifyDataSetChanged();
    }

    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //获得选中条目的结果
    public ArrayList<String> getSelectedItem() {
        ArrayList<String> selectList = new ArrayList<>();
        ArrayList<String> selectListstring = new ArrayList<>();
        ArrayList<String> selectshopsting = new ArrayList<>();

        if (string.equals("shop")) {

            for (int i = 0; i < mList.size(); i++) {
                if (isItemChecked(i)) {
                    selectList.add(mList.get(i).getGuigeid());
                    selectshopsting.add(mList.get(i).getDs());
                }
            }
            SpUtil.putList(MyApplication.getApp(), "checkshop", selectList);
            SpUtil.putList(MyApplication.getApp(), "checkshopstring", selectshopsting);
        } else {
            for (int i = 0; i < mList.size(); i++) {
                if (isItemChecked(i)) {
                    selectList.add(mList.get(i).getMachinenumber());
                    selectListstring.add(mList.get(i).getDetailedinstalladdress());
                }
            }
            SpUtil.putList(MyApplication.getApp(), "checkhis", selectList);
            SpUtil.putList(MyApplication.getApp(), "checkhisstring", selectListstring);
        }

        return selectList;
    }

    //根据位置判断条目是否选中
    public boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //设置给定位置条目的选择状态
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);

        //设置条目状态   滑动时放在onBindViewHolder中有bug  放在onCreateViewHolder  只执行一次
        if (string.equals("machine")) {
            if (mCheckhis != null && mCheckhis.size() > 0) {
                for (int j = 0; j < mCheckhis.size(); j++) {
                    if (mCheckhis.get(j).equals(mList.get(i).getMachinenumber())) {
                        setItemChecked(i, true);
                    }
                }
            }

        } else if (string.equals("shop")) {
            if (mCheckshop != null && mCheckshop.size() > 0) {
                for (int j = 0; j < mCheckshop.size(); j++) {
                    if (mCheckshop.get(j).equals(mList.get(i).getGuigeid())) {
                        setItemChecked(i, true);
                    }
                }
            }
        }
        return new TkSelectAdapter.ListItemViewHolder(itemView);
    }

    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {

        //设置条目状态
        if (string.equals("machine")) {
            ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i).getDetailedinstalladdress());
        } else if (string.equals("shop")) {
            ((ListItemViewHolder) holder).mainTitle.setText(mList.get(i).getDs());
        }

        ((ListItemViewHolder) holder).checkBox.setChecked(isItemChecked(i));

        //checkBox的监听
        ((ListItemViewHolder) holder).checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemChecked(i)) {
                    setItemChecked(i, false);
                } else {
                    setItemChecked(i, true);
                }

            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder {
        //ViewHolder
        CheckBox checkBox;
        TextView mainTitle;

        ListItemViewHolder(View view) {
            super(view);
            this.mainTitle = view.findViewById(R.id.text);
            this.checkBox = view.findViewById(R.id.select_checkbox);

            //设置默认值 防止数据错乱
            this.checkBox.setChecked(false);
        }
    }
}